$(function(){
	page.bind();
	page.xx();
	page.getHead();
	page.getCompany();
	$("#tableBody").html("");
});

var page={
    dateil:"",
    rowLength:0,
    quanImg:"",
    tuiImg:"",
    bind:function(){
    	 $("#printBtn").bind("click",function(){
    		 window.print();
    	 });
    	 
    	 $("#saveBtn").bind("click",function(){
    		 var array=new Array();
    		 var orderId=$("#orderId").val();
    		 $(".rowIndex").each(function(){
    			   var rowIndex=$(this).attr("rowIndex");
    			   $(this).children("td").each(function(index,el){
    				   if(rowIndex==1){
    					   switch(index){
    					      case 0:return true;break;
    					      case 1:return true;break;
    					      case 6:return true;break;
    					      case 7:return true;break;
    					      case 12:return true;break;
    					      case 13:return true;break;
    					      case 14:return true;break;
    					      case 15:return true;break;
    					      case 16:return true;break;
    					   }
    				   }else{
    					   switch(index){
	 					      case 0:return true;break;
	 					      case 1:return true;break;
	 					      case 5:return true;break;
	 					      case 6:return true;break;
	 					      case 10:return true;break;
	 					      case 11:return true;break;
	 					      case 12:return true;break;
	 					      case 13:return true;break;
	 					      case 14:return true;break;
 					      }
    				   }
    				   var object=new Object();
    	    		   object.orderId=orderId;
    	    		   object.rowIndex=rowIndex;
    	    		   object.type=index;
    	    		   object.typeValue=$(el).text();
    	    		   if(object.typeValue!=""){
    	    			   array.push(object);
    	    		   }
    			   });
    		 });
    		 ajaxUtil.ajaxRuquest("orderProcedureTemplate/savePrintDetail",{"printDetailArray":JSON.stringify(array),"orderId":orderId},function(data){
				   layer.msg(data,{icon:1});
			 });
    	 });
    },
    xx:function(){
    	var orderId=$("#orderId").val();
    	$("#code").qrcode({
    		render: "canvas", //table方式
    		width: 100, //宽度
    		height:100, //高度
    		text:"http://www.shinehao.net/weixin/weiXin/sh?orderId="+orderId //任意内容
    	});
    	 var img = document.getElementById("image"); /// get image element
    	 var canvas  = document.getElementsByTagName("canvas")[0];  /// get canvas element
    	 img.src = canvas.toDataURL();                   
    },
    getHead:function(){
    	var orderId=$("#orderId").val();
    	ajaxUtil.ajaxRuquest("orderProcedureTemplate/getOrderMain",{"orderId":orderId},function(data){
    		  var html="<tr>";
    		  html+="<td colspan='31' class='da'> " +
	    		  		"订单号："+ data.customerOrderId+
	    		  		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;厂号："+ data.factoryId+
	    		  		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;交期："+ data.jiaoDate+
	    		  		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;订单数："+ data.orderCount+
	    		  		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;投料数："+ data.feedCount+
	    		  		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;客号："+ data.customerId+
	    		  		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;尺寸："+data.chiCun+
    		        "</td></tr>";
	          page.dateil=data.detail;
	          page.quanImg=data.quanImg;
	          page.tuiImg=data.tuiImg;
	          $("#tableBody").append(html);
	          $("#keKuanHao").text(data.keKuanHao);
	          page.getMaterialDetail();
    	});
    },
    getMaterialDetail:function(){
    	var orderId=$("#orderId").val();
    	ajaxUtil.ajaxRuquest("orderProcedureTemplate/getMaterialDetail",{"orderId":orderId},function(data){
    		var length=data.length;
    		page.rowLength=length;
    		var html="<tr>"+
				        "<td>圈色</td>"+      
		                "<td>编号</td>"+
		                "<td>每条料可开数/付</td>"+
		                "<td>开料方式</td>"+
		                "<td>用料数/条</td>"+
		                "<td>圈料开料尺寸</td>"+
		                "<td>脾色</td>"+
		                "<td>编号</td>"+
		                "<td>每条料可开数/付</td>"+
		                "<td>开料方式</td>"+
		                "<td>用料数/条</td>"+
		                "<td>脾料开料尺寸</td>"+
		                "<td>金属框色描述</td>"+
		                "<td>金属脚色描述</td>"+
		                "<td>金属饰片描述</td>"+
		                "<td>订单数</td>"+
		                "<td>投料数</td>"+
		                "<td>配件数</td>"+
		                "<td>领料</td>"+
		                "<td>开圈料</td>"+
		                "<td>开脾料</td>"+
		                "<td>车房</td>"+
		                "<td>脾部</td>"+
		                "<td>出桶</td>"+
		                "<td>水晶胶色</td>"+
		                "<td>雕刻</td>"+
		                "<td>裝搭</td>"+
		                "<td>抛光</td>"+
		                "<td>点漆色</td>"+
		                "<td>烫印色</td>"+
		                "<td>成品</td>"+
		             "</tr>";
    		html+="<tr class='rowIndex' rowIndex='"+data[0].rowIndex+"'>"+
		                "<td>"+data[0].quanSeMiao+"</td>"+
		                "<td>"+data[0].quanSe+"</td>"+
		                "<td>"+data[0].quanmtlktTotal+"</td>"+
		                "<td>"+data[0].quanklfsWay+"</td>"+
		                "<td>"+data[0].quanyltsTotal+"</td>"+
		                "<td rowspan='"+length+"'>"+data[0].quanklccSize+"</td>"+
		                "<td>"+data[0].biSeMiao+"</td>"+
		                "<td>"+data[0].biSe+"</td>"+
		                "<td>"+data[0].tuimtlktTotal+"</td>"+
		                "<td>"+data[0].tuikylfsWay+"</td>"+
		                "<td>"+data[0].tuiltsTotal+"</td>"+
		                "<td rowspan='"+length+"'>"+data[0].tuiklccSize+"</td>"+
		                "<td>"+data[0].jinQuanSeMiao+"</td>"+
		                "<td>"+data[0].jinBiSeMiao+"</td>"+
		                "<td>"+data[0].jinShuShiPian+"</td>"+
		                "<td>"+data[0].orderCount+"</td>"+
		                "<td>"+data[0].feedCount+"</td>"+
		                "<td></td>"+
		                "<td></td>"+
		                "<td></td>"+
		                "<td></td>"+
		                "<td></td>"+
		                "<td></td>"+
		                "<td></td>"+
		                "<td>"+data[0].shuiJingJaio+"</td>"+
		                "<td></td>"+
		                "<td></td>"+
		                "<td></td>"+
		                "<td>"+data[0].dianQiSe+"</td>"+
		                "<td>"+data[0].tangYinSe+"</td>"+
		                "<td></td>"+
		             "</tr>";
    		for(var i=1;i<length;i++){
    			html+="<tr class='rowIndex' rowIndex='"+data[i].rowIndex+"'>"+
    			"<td>"+data[i].quanSeMiao+"</td>"+
                "<td>"+data[i].quanSe+"</td>"+
                "<td>"+data[i].quanmtlktTotal+"</td>"+
                "<td>"+data[i].quanklfsWay+"</td>"+
                "<td>"+data[i].quanyltsTotal+"</td>"+
                "<td>"+data[i].biSeMiao+"</td>"+
                "<td>"+data[i].biSe+"</td>"+
                "<td>"+data[i].tuimtlktTotal+"</td>"+
                "<td>"+data[i].tuikylfsWay+"</td>"+
                "<td>"+data[i].tuiltsTotal+"</td>"+
                "<td>"+data[i].jinQuanSeMiao+"</td>"+
                "<td>"+data[i].jinBiSeMiao+"</td>"+
                "<td>"+data[i].jinShuShiPian+"</td>"+
                "<td>"+data[i].orderCount+"</td>"+
                "<td>"+data[i].feedCount+"</td>"+
                "<td></td>"+
                "<td></td>"+
                "<td></td>"+
                "<td></td>"+
                "<td></td>"+
                "<td></td>"+
                "<td></td>"+
                "<td>"+data[i].shuiJingJaio+"</td>"+
                "<td></td>"+
                "<td></td>"+
                "<td></td>"+
                "<td>"+data[i].dianQiSe+"</td>"+
                "<td>"+data[i].tangYinSe+"</td>"+
                "<td></td>"+
                "<td></td>"+
		      "</tr>";
    		}
    		html+="<tr><td colspan='8' rowspan='3'><img src='"+page.quanImg+"' /></td><td colspan='8' rowspan='3'><img src='"+page.tuiImg+"' /></td><td colspan='2'>日期</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>";
    		html+="<tr><td colspan='2'>交货人</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>";
    		html+="<tr><td colspan='2'>收货人</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>";
    		$("#tableBody").append(html);
//    		for(var i=0;i<length;i++){
//    			var detail=data[i].printList;
//    			for(var j=0;j<detail.length;j++){
//    				$("[rowIndex="+detail[j].rowIndex+"]").children("td").eq(detail[j].type).text(detail[j].typeValue);
//    			}
//    		}
    		page.getProcedure();
    	});
    },
    getProcedure:function(){
    	var orderId=$("#orderId").val();
    	ajaxUtil.ajaxRuquest("orderProcedureTemplate/orderMagerProcedure",{"orderId":orderId},function(data){
    		var html="";
    		var length=data.length;
    		var yu=16-(length*4);
    		var maxLength=0;
//    		if(length<4){
//    			yu=yu-1;
//    		}
    		if(length<=4){
    			var htmlhead="<tr>";
    			var htmlhead2="<tr>";
    			for(var i=0;i<length;i++){
//    				if(i!=4){
    					htmlhead+="<td colspan='4' class='shopFont'>"+data[i].workshopName+"</td>";
    					htmlhead2+="<td>产品编码</td><td>数量</td><td>操作人</td><td>特别说明</td>";
//    				}
//    				if(i==4){
//    					htmlhead+="<td colspan='5' class='shopFont'>"+data[i].workshopName+"</td>";
//    					htmlhead2+="<td>产品编码</td><td>数量</td><td>操作人</td><td>部门</td><td>特别说明</td>";
//    				}
					if(data[i].processList.length>maxLength){
						maxLength=data[i].processList.length;
					}
    			}
    			for(var i=0;i<yu;i++){
    				htmlhead+="<td></td>";
    				htmlhead2+="<td></td>";
    			}
    			maxLength=maxLength+1;
    			htmlhead+="<td colspan='15'>订单信息</td>";
    			htmlhead2+="<td colspan='15' rowspan='"+maxLength+"'><div style='width:650px;'>"+page.dateil+"</div></td>";
    			html+=htmlhead+"</tr>"+htmlhead2+"</tr>";
    		}else{
    			
    			
    		}
    		
    		$("#tableBody").append(html);
    		for(var i=0;i<length;i++){
    			var processList=data[i].processList;
    			for(var j=0;j<processList.length;j++){
    				var tableRow=$("#tableBody").find("tr").length;
    				var dd=10+page.rowLength+j;
    				
    				if(tableRow<dd-2){
    					$("#tableBody").append(entmpTr);
    					dd=tableRow;
//    					if(i!=0){
//    						dd=dd-2;
//    					}
    				}
    				else{
    					dd=dd-3;
    				}
    				var vv=i*4;
    				$("#tableBody").find("tr").eq(dd).find("td").eq(vv).text(processList[j].processName);
    			}
    		}
    		$("td").attr("contentEditable",true);
    	});
    },
	getCompany:function(){
		SystemUtil.getUser().then(function(user){
			$("#companyName").html(user.company);
		});
	}
}

var entmpTr="<tr><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td><td class='le'></td></tr>";

